/*
 *  IOCOM.cpp
 *  Ndist
 *
 *  Created by Diego Andrade on 1/27/11.
 *  Copyright 2011 Carnegie Mellon University. All rights reserved.
 *
 */

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include "IOCOM.h"
#include "matrix.h"


using namespace std;


IOEVENTS::IOEVENTS()
{
    n1 = 0;
    n2 = 0;
    n3 = 0;
    n4 = 0; //number of elements
}

IOEVENTS::~IOEVENTS()
{


}

void IOEVENTS::io_read_mpxtxt_files (void)
{
        cout << "Hello, World!\n";
        
        int length;
        
        
        char * buffer;
        
        ifstream is;
        is.open ("geo1.mphtxt", ios::binary );
        
        // get length of file:
        is.seekg (0, ios::end);
        length = is.tellg();
        is.seekg (0, ios::beg);
        
        // allocate memory:
        buffer = new char [length];
        
        // read data as a block:
        is.read (buffer,length);
        is.close();
        
        cout.write (buffer,length);
        
        delete[] buffer;

}

void IOEVENTS::io_read_mpxtxt_line(char* file)
{
        cout << "Entering IOEVENTS::io_read_mpxtxt_line\n";
        
        int length = 0;
        
        char str[255] ="";
        char str1[255] = "";
        char str2[255] = "# Mesh point coordinates";
        char str3[255] = "3 # number of nodes per element";
        char str4[255] = "# Elements";
       
        char * buffer;
        
               
            A = new int[2000];
            B = new int[2000];
            C = new int[2000];
              
        coordinates_2D * m_coordinates = new coordinates_2D[2000];       
        triangle_vertices_structure * m_vertices = new triangle_vertices_structure[2000];
         
        MV = io_matrix(2000);
         
        float a;
        
        int count_lin = 0;
        int i = 0;
        int j =0;
        
        ifstream is;
        is.open (file, ios::binary );
        
        ifstream inf;
        inf.open (file, ios::binary );
        
        // get length of file:
        is.seekg (0, ios::end);
        length = is.tellg();
        is.seekg (0, ios::beg);
        
        // allocate memory:
        buffer = new char [length];
        
        //flags
        bool _flg_mesh_coordinates =false;
        bool _flg_mesh_3_nbr_elements =false;


//CAUTION: with str not changing the values because is not correctly initialize
// I am not erasing the values of char str str1 str2 
// that could cause problems

//Reading the number of elements from file I use inf to capture that number
 /*  while(!inf.getline(str, 255).eof())
   {    
        
            while(i<30)
            {
            is.getline(str1,255);
                if(strncmp (str1,str3,10) == 0)
                {
                    _flg_mesh_nbr_elements = true;  
                    cout << str1; 
                }  
                if(_flg_mesh_nbr_elements == true)
                {
                    
                }
            i++;
            }
        
   }

*/

   while(!is.getline(str1, 255).eof())
  {
      
        
                
             //is.getline(str1,255);
             if(strncmp (str1,str2,10) == 0)
             {
              _flg_mesh_coordinates = true;
              
              //cout << str1; 
            }  
             if(_flg_mesh_coordinates == true)
             {
                  
                 //cout << "\n" <<i<<endl;
                 
                 //cout << str1 <<endl;      
                 // if(is) cout<< str1 << endl;
 
                       
                         sscanf(str1,"%f %f",&coor.x,&coor.y);
                         
                     if(i<1800) //<<----change here no hardcode
                     {
                         m_coordinates[i-1].x = coor.x;
                         m_coordinates[i-1].y = coor.y;
                                                         
                       // cout << "X :" << m_coordinates[i-1].x << endl;
                       // cout << "Y :" << m_coordinates[i-1].y << endl;
                        
                         MV[0][i-1]=coor.x;
                         MV[1][i-1]=coor.y;
                     }
                     
                  i++;
                  //if(i==1) i=0;  
                  //if(i>1642)  _flg_mesh_coordinates == false;
             }
             
          
            if(strncmp (str1,str3,30) == 0)
                {
                    _flg_mesh_3_nbr_elements = true;  
                    _flg_mesh_coordinates = false;
                    cout<< str1 << endl;                 
                }  
            
             if(_flg_mesh_3_nbr_elements == true && j < 2000) //<--- change here no hardcode
             {
                  
                // cout << " \nj: " <<j<<endl;
                // cout << str1 <<endl;  
                 
                 sscanf(str1,"%d %d %d",&ver.a,&ver.b,&ver.c);
                 
                 //cout << ver.a <<endl;
                 // cout << ver.b <<endl;
                 //  cout << ver.c <<endl;
            
                 if (j>2)
                 {
                    A[j-3] = ver.a;// cout << "A[" << j-3 << "]: " <<  ver.a  << ", ";
                    B[j-3] = ver.b;// cout << "B[" << j-3 << "]: " <<  ver.b  << ", ";
                    C[j-3] = ver.c; //cout << "C[" << j-3 << "]: " <<  ver.c  << ", ";
                        
                }
                 j++;   

             }    
                
                
                
           // i++;
            //cout << i; 
      
                
            
            
    }
    


      is.close(); 
      inf.close();   

        

         //    system("pause");
        delete[] buffer;
        
}

//Inizialize a square matrix of SIZE
float** IOEVENTS::io_matrix(int SIZE)
{
	float **M = new float * [SIZE];
	for(int i=0;i<2;i++){
		M[i] = new float  [SIZE];
		for(int j=0;j<SIZE;j++){
			M[i][j] = 0.0;
			//M[i][j] = M[i][j];
		}
		
	}

	return M;
}

